# Copyright (c) 2008 Alvin Wang http://cloudwizard.com/
# License New BSD
# See README for additional information

"""
Parameters
-h = help
"""
__version__ = "0.02"
__copyright__ = "Copyright 2008 Alvin Wang"
__author__ = "Alvin Wang <alvin@cloudwizard.com>"
__date__ = "Fri Feb 6 14:26:00 PST 2009"
__license__ = "New BSD"
__all__ = ["aws","test"]

import os, logging
import ConfigParser
from optparse import OptionParser as OrigOptionParser
Version = '0.01'
from cloudwizard.aws import Aws
#from cloudwizard.dreamhost import Dreamhost
from cloudwizard.slicehost import Slicehost
from cloudwizard.gogrid import GoGrid

"""
parser = OptionParser()
parser.add_option("-f", "--file",
	action="store", type="string", dest="filename",help="log filename")
parser.add_option("-d", "--debug", dest="debug",
                  help="set debug level", metavar="FILE")
parser.add_option("-c", "--quiet",
                  action="store_false", dest="verbose", default=True,
                  help="don't print status messages to stdout")
(options, args) = parser.parse_args()
##### get parms/script from main site
"""

"""
for i in range(20):
	log.debug('i = %d' % i)
log.debug("debug message")
log.info("info message")
log.warn("warn message")
log.error("error message")
log.critical("critical message")
print "CW initialize2"
"""
class Cloudwizard(object):
    cloud_list = ['aws','dreamhost','facebook','gogrid','mosso','slicehost']
    def __init__(self, **args):
        pass

    def is_cloud(self, cloud_name=None, **args):
        """Print list of clouds"""
        if cloud_name in self.cloud_list:
            return True
        return False

    def get_cloud(self, cloud_name='aws', autoload=True, username='default',**args):
        """Return cloud object"""
        if cloud_name=='aws':
            return Aws(username=username, autoload=autoload)
        if cloud_name=='gogrid':
            return GoGrid(username=username, autoload=autoload)
        if cloud_name=='slicehost':
            return Slicehost(username=username, autoload=autoload)
        pass
        
    def get_object(self, config=None, cloud_name=None, **args):
        """Return variable object"""
        pass
        
    def olist(self, cloud_name=None, **args):
        """Return list of clouds"""
        return self.cloud_list
    
    def plist(self, cloud_name=None, **args):
        """Print list of clouds"""
        clouds = self.olist(cloud_name=cloud_name)
        for cloud in clouds:
            print cloud
    
    

class OptionParser( OrigOptionParser):
    """Enhanced Option Parser
    required is array of required options - prompts too
    """
    def __init__(self, required=None, usage=None, version=__version__):
        OrigOptionParser.__init__(self, usage=usage, version=version)
        if usage == 'onecloud':
            self.add_option("-u", "--user", dest="username", default='default',
                              help="set the cloud authentication user, use init to create new user")
        else:            
            self.add_option("-f", "--file", dest="filename",
                              help="read script from file")
                              
            self.add_option("-u", "--user", dest="username", default='default',
                              help="set the cloud authentication user")
                              
            self.add_option("-c", "--cloud", dest="cloudname",
                              help="cloud name")
                              
            self.add_option("-s", "--service", dest="servicename",
                              help="service name")
                              
            self.add_option("-m", "--machine", dest="machinename",
                              help="machine name")
                              
            self.add_option("-i", "--init", dest="init", default=False,
                              help="initialize the cloud authentication")

    def auth(self, cloud_name=None):
        """call the init for the cloud"""
        pass
        
    def parse_config(self, config_type=None):
        """Parse Arguments and read config files"""
        (options, args) = self.parse_args()
        config={}
        return (options, args, config)

    def print_listdict_col(self, result=None):
        """Print a list of dict"""
        for row in result:
            for k,v in row.iteritems():
                print k,v        
            print

    def print_listdict_row(self, result=None):
        """Print a list of dict"""
        i=0
        for row in result:
            if i== 0:
                for k,v in row.iteritems():
                    print k,        
                print
            for k,v in row.iteritems():
                print v,        
            print
            i=1
        """ Logging
        globalVars = 0
        LOG_FILE ="/tmp/alvin.log"
        log = logging.getLogger(LOG_FILE)

        log.setLevel(logging.DEBUG)

        #create file handler and set level to debug
        fh = logging.FileHandler(LOG_FILE)
        fh.setLevel(logging.DEBUG)

        #create formatter
        formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        #add formatter to ch and fh

        fh.setFormatter(formatter)
        #add ch and fh to logger
        log.addHandler(fh)
        """